﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using System.Net.Http.Formatting;
using vncom;
using vncom.Models;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;

namespace vncom.Controllers
{
    public class WebservicesController : ApiController
    {
        //
        // GET: /Webservices/

        [HttpGet]
        public HttpResponseMessage GetAllProvinces()
        {
            IContentNegotiator config = this.Configuration.Services.GetContentNegotiator();
            ContentNegotiationResult result = config.Negotiate(
                typeof(List<SerializeProvince>), this.Request, this.Configuration.Formatters);
            var listProvince = DataProvider.SelectStoreProcedure("GetAllProvinces");
            List<SerializeProvince> serial = new List<SerializeProvince>();
            SerializeProvince header = new SerializeProvince()
            {
                ProvinceID = "0",
                ProvinceName = "[Chọn tỉnh]",
                Longitude = "106.662",
                Latitude = "10.75783"
            };
            serial.Add(header);
            foreach (DataRow item in listProvince.Rows)
            {
                //DataColumn col1 = listProvince.Columns["ProvinceID"];
                SerializeProvince newObj = new SerializeProvince()
                {
                    ProvinceID = item["ProvinceID"].ToString(),
                    ProvinceName = item["ProvinceName"].ToString(),
                    Longitude = item["Longitude"].ToString(),
                    Latitude = item["Latitude"].ToString(),
                };
                serial.Add(newObj);
            }
            return new HttpResponseMessage()
            {
                Content = new ObjectContent<List<SerializeProvince>>(serial, result.Formatter, result.MediaType.MediaType)
            };
        }

        [HttpGet]
        public HttpResponseMessage GetDByP(string id)
        {
            IContentNegotiator config = this.Configuration.Services.GetContentNegotiator();
            ContentNegotiationResult result = config.Negotiate(
                typeof(List<SerializeDistrict>), this.Request, this.Configuration.Formatters);
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@pro", SqlDbType.Int);
            p1.Value = int.Parse(id);
            listPara.Add(p1);
            var listProvince = DataProvider.SelectStoreProcedure("GetDisByPro",listPara);
            List<SerializeDistrict> serial = new List<SerializeDistrict>();
            SerializeDistrict header = new SerializeDistrict()
            {
                DistrictID = "0",
                DistrictName = "[Chọn quận/huyện]"
            };
            serial.Add(header);
            foreach (DataRow item in listProvince.Rows)
            {
                //DataColumn col1 = listProvince.Columns["ProvinceID"];
                SerializeDistrict newObj = new SerializeDistrict()
                {
                    DistrictID = item["DistrictID"].ToString(),
                    DistrictName = item["DistrictName"].ToString(),
                };
                serial.Add(newObj);
            }
            return new HttpResponseMessage()
            {
                Content = new ObjectContent<List<SerializeDistrict>>(serial, result.Formatter, result.MediaType.MediaType)
            };
        }

        [HttpGet]
        public HttpResponseMessage VerifyUsername(string id)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = id;
            listPara.Add(p1);
            SqlParameter resultProc = new SqlParameter("@result", SqlDbType.Bit);
            resultProc.Value = "0";
            var cmd = DataProvider.OutPutProc("CheckUser", listPara, resultProc);
            if (resultProc.Value.ToString() == "True")
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("no")
                };
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
        }

        [HttpGet]
        public HttpResponseMessage VerifyEmail(string id)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@email", SqlDbType.VarChar);
            p1.Value = id;
            listPara.Add(p1);
            SqlParameter resultProc = new SqlParameter("@result", SqlDbType.Bit);
            resultProc.Value = "0";
            var cmd = DataProvider.OutPutProc("CheckEmail", listPara, resultProc);
            if (resultProc.Value.ToString() == "True")
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("no")
                };
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
        }

        [HttpGet]
        public HttpResponseMessage VerifyDomain(string id)
        {
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@domain", SqlDbType.VarChar);
            p1.Value = id;
            listPara.Add(p1);
            SqlParameter resultProc = new SqlParameter("@result", SqlDbType.Bit);
            resultProc.Value = "0";
            var cmd = DataProvider.OutPutProc("CheckDomain", listPara, resultProc);
            if (resultProc.Value.ToString() == "True")
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("no")
                };
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
        }

        [HttpGet]
        public HttpResponseMessage PostLabview(string time, string tru, string id, string lit, string tien, string gia, string auto, string total)
        {
            try
            {
                int d_time = int.Parse(time);
                int d_tru = int.Parse(tru);
                int d_id = int.Parse(id);
                float d_lit = float.Parse(lit);
                int d_gia = int.Parse(gia);
                int d_tien = int.Parse(tien);
                bool d_auto = auto == "a" ? true : false;
                float d_total = float.Parse(total);
            }
            catch (Exception e)
            {

            }
            
            return new HttpResponseMessage()
            {
                Content = new StringContent("ok")
            };
        }

    }
}
